import { request } from "../request";

const token = localStorage.getItem("token");

// 数据接口定义
export interface Classes {
    classId?: number;
    name?: string;
    description?: string;
    createdAt?: Date;
    // 根据你的实际Classes实体添加其他字段
}

export interface ResponseEntity<T> {
    code: number;
    message: string;
    data: T;
    success: boolean;
}

// 获取所有班级信息
const getAllClassesAPI = async (): Promise<ResponseEntity<Classes[]>> => {
    const token = localStorage.getItem("token");
    return request("/api/classes/getAllClasses", {
        method: "GET",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'Authorization': `Bearer ${token}`
        },
    });
};

// 创建班级
const createClassAPI = async (classes: Classes): Promise<ResponseEntity<string>> => {
    const token = localStorage.getItem("token");
    return request("/api/classes/createClass", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            'Authorization': `Bearer ${token}`
        },
        data: classes
    });
};

// 更新班级信息
const updateClassByIdAPI = async (classes: Classes): Promise<ResponseEntity<string>> => {
    const token = localStorage.getItem("token");
    return request("/api/classes/updateClassById", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            'Authorization': `Bearer ${token}`
        },
        data: classes
    });
};

// 删除班级
const deleteClassByIdAPI = async (classId: number): Promise<ResponseEntity<string>> => {
    const token = localStorage.getItem("token");
    return request("/api/classes/deleteClassById", {
        method: "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'Authorization': `Bearer ${token}`
        },
        data: classId
    });
};

// 获取班级信息
const getClassByIdAPI = async (classId: number): Promise<ResponseEntity<Classes>> => {
    const token = localStorage.getItem("token");
    return request(`/api/classes/getClassById?classId=${classId}`, {
        method: "GET",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded",
            'Authorization': `Bearer ${token}`
        },
    });
};

// 导出所有API
export {
    getAllClassesAPI,
    createClassAPI,
    updateClassByIdAPI,
    deleteClassByIdAPI,
    getClassByIdAPI
};
